Utility Scripts
General-purpose utilities for working with geospatial raster data, including metadata generation, statistical analysis, and size calculations.gdal2metadata.py
Generates FGDC (Federal Geographic Data Committee) metadata XML from GDAL-supported raster files.Usage
Parameters
Input georeferenced raster file
Input FGDC metadata template XML file
Output populated FGDC metadata XML file
Options
Print detailed image information during processing
Compute and report min/max values
Compute and report statistics
Report histograms
Metadata Fields Populated
Automatically extracts and populates:Spatial Information
- Coordinate system - Projection name and parameters
- Datum and spheroid - Target body, radii, flattening
- Resolution - Pixel size in degrees or meters
- Extent - Bounding coordinates (westbc, eastbc, northbc, southbc)
- Image dimensions - Rows and columns
Projection Parameters
Supports and extracts parameters for:- Equirectangular
- Mercator
- Transverse Mercator
- Sinusoidal
- Robinson
- Stereographic
- Polar Stereographic
- Orthographic
Technical Details
- Data type and bit depth
- NoData values
- Scale and offset
Example
Template File
Requires an FGDC XML template with placeholder elements. The script populates:Coordinate Normalization
Longitudes are automatically normalized to -180 to 180 range for FGDC validation compliance.
Requirements
gdal_hist.py
Exports raster histogram data in tab-delimited format for analysis and visualization.Usage
Parameters
Input raster dataset
Options
Compute and display min/max values
Compute and display statistics (min, max, mean, stddev, RMS)
Export histogram data (required)
Apply scale and offset to unscale values to original units
At least one flag (
-mm, -stats, or -hist) must be specified.Output Format
Statistics Output
Histogram Output
- level - Bin number
- value - Center value of bin
- count - Number of pixels in bin
- cumulative - Cumulative percentage (0-1)
Example
Scale and Offset
When-unscale is used:
Multi-band Files
For multi-band files, each band is processed separately:slope_histogram_cumulative_graph.py
Creates histogram and cumulative distribution visualizations from tabular histogram data.Usage
Parameters
Input tab-delimited histogram file (from gdal_hist.py)
Output PNG image file
Options
Title for the plot
Input File Format
Expects tab-delimited format fromgdal_hist.py:
Output Visualization
Generates a dual-axis plot:- Left Y-axis - Frequency histogram (gray filled area)
- Right Y-axis - Cumulative distribution (blue line)
- X-axis - Value (typically slope in degrees)
- Vertical line - Reference line at x=15 (suitable for slopes)
Example
Customization
The script can be modified to:- Change reference line position (default: x=15)
- Adjust color scheme
- Modify axis labels
- Change figure size
Requirements
gdalSize.py
Calculates uncompressed raster file size based on geographic extent and resolution.Usage
Parameters
Minimum longitude (degrees)
Minimum latitude (degrees)
Maximum longitude (degrees)
Maximum latitude (degrees)
Resolution in meters per pixel
Bit depth: 8, 16, or 32
Number of bands
Reference image for projection information
Output
Reports estimated file size:How It Works
- Reads projection from reference image
- Transforms geographic bounds to projected coordinates
- Calculates dimensions based on resolution
- Computes uncompressed size:
lines × samples × bands × bytes_per_pixel
Size Calculation
| Bit Depth | Bytes per Pixel |
|---|---|
| 8 | 1 |
| 16 | 2 |
| 32 | 4 |
Example
Use Cases
- Storage planning - Estimate disk space before processing
- Data ordering - Calculate download sizes
- Processing planning - Determine memory requirements
- Cost estimation - Calculate cloud storage costs
Limitations
- Calculates uncompressed size only
- Actual compressed size varies by format and compression
- Does not account for tile/block overhead
gdal2AsciiLatLonBands.py
Exports raster band data to ASCII/CSV format with optional latitude/longitude or XY coordinate columns.Usage
Parameters
Input raster file
Output ASCII/CSV file (writes to stdout if not specified)
Options
Extract subset window (offsets and dimensions in meters)
Band number to export (can be specified multiple times for multiple bands). Defaults to band 1.
Add header row with field names
Include Lat/Lon columns (uses GDAL projection to calculate)
Include Y,X columns in meters
Examples
Output Format
Without coordinates:Use Cases
- Point sampling - Extract band values at specific locations
- Data integration - Create CSV for import to databases or spreadsheets
- Validation - Compare pixel values across different datasets
- Analysis input - Generate point clouds for statistical analysis
Common Workflows
Complete Slope Analysis Workflow
Metadata Documentation Workflow
Data Planning Workflow
Installation
Basic Installation
Full Installation (All Utilities)
Verify Installation
Requirements Summary
| Script | Python | GDAL | NumPy | Pandas | Matplotlib | lxml |
|---|---|---|---|---|---|---|
| gdal2metadata.py | 2.7+ | ✓ | - | - | - | ✓ |
| gdal_hist.py | 2.7+ | ✓ | - | - | - | - |
| slope_histogram_cumulative_graph.py | 3.x | - | - | ✓ | ✓ | - |
| gdalSize.py | 2.7+ | ✓ | - | - | - | - |
| gdal2AsciiLatLonBands.py | 2.7+ | ✓ | - | - | - | - |
Troubleshooting
ImportError: No module named 'osgeo'
ImportError: No module named 'osgeo'
Install GDAL Python bindings:
Metadata validation errors
Metadata validation errors
Ensure:
- Input file has valid projection
- Template XML is valid FGDC format
- Longitude values are in -180 to 180 range
Histogram visualization fails
Histogram visualization fails
Check:
- Input file is tab-delimited
- Contains required columns: level, value, count, cumulative
- matplotlib backend is properly configured
Size calculation seems incorrect
Size calculation seems incorrect
Verify:
- Reference image has correct projection
- Bounds are in correct order (min before max)
- Resolution units match projection units